package model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.JoinColumn;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * This class enables the CRUD (Create/Read/Update/Delete) operations in leagues.
 * The main attributes of the league are:
 * - Name of the league
 * - User who is League Operator
 * - Creation date
 *  
 * @author Andre Melo
 * @author Marcus Leite 
 * @author Sergio Danilo
 */
@Entity
@Table(name = "LEAGUES")
public class League {

	//Attributes
	//Columns for Data Base manipulation
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Integer id;

	@Column(name = "NAME", nullable = false, length = 50)
	private String name;

	@ManyToOne
	private User operator;

	@Temporal(TemporalType.DATE)	
	@Column(name = "CREATION_DATE", nullable = true) //TODO change to false after
	private Date creationDate;

	@Column(name = "NUMBER_OF_MATCHES", nullable = false)
	private Integer numberOfMatches = 0;
	
	@Column(name = "STANDING_EXPRESSION", nullable = true)
	private String standingExpression = "";
	
	@org.hibernate.annotations.CollectionOfElements
	@JoinTable(name="JOIN_LEAGUES", joinColumns=@JoinColumn(name="LEAGUE_ID"))
	private List<JoinLeague> joinLeagues = new ArrayList<JoinLeague>();
	
	/**
	 * League's constructor 
	 */
	public League() {}

	/**
	 * League's constructor with parameters
	 * 
	 * @param name Name of the league
	 * @param operator League's operator
	 */
	public League(String name, User operator) {
		super();
		this.name = name;
		this.operator = operator;
		this.creationDate = new Date();
	}

	/**
	 * Get the creation date of the league
	 * @return The date format
	 */
	public Date getCreationDate() {
		return creationDate;
	}

	/**
	 * Get the ID of the league for Date Base manipulation
	 * @return The DB ID
	 */
	public Integer getId() {
		return id;
	}

	/**
	 * Get a list with the leagues who joined
	 * @return The joined leagues
	 */
	public List<JoinLeague> getJoinLeagues() {
		return joinLeagues;
	}

	/**
	 * Get the name of the league
	 * @return The name of the league
	 */
	public String getName() {
		return name;
	}

	/**
	 * Get the operator of the league
	 * @return An user that represents the operator of the league
	 */
	public User getOperator() {
		return operator;
	}

	/**
	 * Set the creation date of the league 
	 * @param creationDate The new creation date
	 */
	public void setCreationDate(Date creationDate) {
		this.creationDate = creationDate;
	}

	/**
	 * Set the ID of the league for Data Base manipulation
	 * @param id The new DB ID
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * Set the joined leagues
	 * @param joinLeagues The new joined leagues
	 */
	public void setJoinLeagues(List<JoinLeague> joinLeagues) {
		this.joinLeagues = joinLeagues;
	}

	/**
	 * Set the name of the league
	 * @param name The new name
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Set the operator of the league
	 * @param operator The new operator (an User) 
	 */
	public void setOperator(User operator) {
		this.operator = operator;
	}

	/**
	 * Get the numbers of matches in the league
	 * @return matches in the league
	 */
	public Integer getNumberOfMatches() {
		return numberOfMatches;
	}

	/**
	 * Set the numbers of matches in the league, with received parameter
	 * @param numberOfMatches The new number of matches
	 */
	public void setNumberOfMatches(Integer numberOfMatches) {
		this.numberOfMatches = numberOfMatches;
	}
	
	/**
	 * Increment the numbers of matches in the league
	 */
	public void setNumberOfMatches() {
		this.numberOfMatches++;
	}

	/**
	 * @return the standingExpression
	 */
	public String getStandingExpression() {
		return standingExpression;
	}

	/**
	 * @param standingExpression the standingExpression to set
	 */
	public void setStandingExpression(String standingExpression) {
		this.standingExpression = standingExpression;
	}
	
}
